var maxPathSum = function (root) {
    let maxSum = -Infinity;
    function dfs(node) {
        if (!node) return 0;

        let left = Math.max(dfs(node.left), 0);
        let right = Math.max(dfs(node.right), 0);

        maxSum = Math.max(maxSum, left + right + node.val);

        return Math.max(left, right) + node.val;
    };
    dfs(root);
    return maxSum;
}